import { getToken } from '@/utils/auth.js'
export default ({ $axios, redirect, req, $cookies }) => {
  $axios.defaults.withCredentials = true

  $axios.defaults.timeout = 60000
  $axios.defaults.baseURL = 'http://dyapi.shenzhenbenwo.com/'
  // 服务器返回异常拦截·
  $axios.onError(error => {
    redirect(error)
  })
  $axios.onRequest(
    config => {
      const cookie = $cookies.get('_token')
      if (!cookie) {
        config['Authorization'] = cookie
      } else {
        config.headers.authorization = cookie
      }

      return config
    },
    error => {
      return Promise.reject(error)
    }
  )
  // 接口数据返回拦截
  $axios.onResponse(
    response => {
      const res = response.data
      // 状态码异常 跳转到登陆
      return Promise.resolve(res)
    },
    error => {
      return Promise.reject(error)
    }
  )
}
